/*
 * ImportGraph.java
 *
 * Created on June 18, 2007, 9:57 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster;

import com.vividsolutions.jts.geom.Coordinate;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;
import org.fpdev.apps.rtemaster.gui.map.MapDrawable;
import org.opengis.feature.simple.SimpleFeature;

/**
 *
 * @author demory
 */
public class ImportGraph implements MapDrawable {

  private List<Link> links_;

  public ImportGraph() {
    links_ = new LinkedList<Link>();
  }

  public void addLink(Link link) {
    links_.add(link);
  }

  public List<Coordinate> endPoints() {
    List<Coordinate> endPts = new ArrayList<Coordinate>();
    Iterator<Link> links = links_.iterator();
    while (links.hasNext()) {
      Link link = links.next();
      endPts.add(link.start());
      endPts.add(link.end());
    }
    return endPts;
  }

  public int linkCount() { return links_.size(); }
  
  public Iterator<Link> links() {
    return links_.iterator();
  }

  public void draw(MapCanvas canvas) {
    Iterator<Link> links = links_.iterator();
    while (links.hasNext()) {
      canvas.setLineWidth(4);
      canvas.setColor(Color.BLUE);
      links.next().draw(canvas);
    }
  }

  public Rectangle2D getBoundingBox() {
    if (links_.isEmpty()) {
      return null;
    }

    Iterator<Link> links = links_.iterator();
    Rectangle2D rect = links.next().getBoundingBox();
    while (links.hasNext()) {
      Link link = links.next();
      if (link != null) {
        rect = rect.createUnion(link.getBoundingBox());
      }
    }
    return rect;
  }

  // INTERNAL CLASSES
  public static class Link {

    private List<Coordinate> coords_;

    private SimpleFeature feature_;

    /*public Link(List<Coordinate> coords) {
      coords_ = coords;
    //System.out.println("coord count: "+coords_.size());
    }

    public Link(Coordinate[] coords) {
      coords_ = new ArrayList<Coordinate>();
      for (int i = 0; i < coords.length; i++) {
        coords_.add(coords[i]);
      }
    //System.out.println("coord count: "+coords_.size());
    }*/


    public Link(Coordinate[] coords, SimpleFeature f) {
      coords_ = new ArrayList<Coordinate>();
      for (int i = 0; i < coords.length; i++) {
        coords_.add(coords[i]);
      }
      feature_ = f;
    //System.out.println("coord count: "+coords_.size());
    }

    public SimpleFeature getFeature() {
      return feature_;
    }

    public void draw(MapCanvas canvas) {
      Iterator<Coordinate> coords = coords_.iterator();
      Coordinate c1 = coords.next();
      while (coords.hasNext()) {
        Coordinate c2 = coords.next();
        canvas.drawLine(c1.x, c1.y, c2.x, c2.y);
        c1 = c2;
      }
    }

    public Coordinate start() {
      return coords_.get(0);
    }

    public Coordinate end() {
      return coords_.get(coords_.size() - 1);
    }

    public List<Point2D.Double> shapePoints() {
      List<Point2D.Double> shpPts = new LinkedList<Point2D.Double>();
      if (coords_.size() <= 2) {
        return shpPts;
      }
      for (int i = 1; i < coords_.size() - 1; i++) {
        shpPts.add(new Point2D.Double(coords_.get(i).x, coords_.get(i).y));
      }
      //System.out.println("ret shpPts, count="+shpPts.size());
      return shpPts;
    }

    public Rectangle2D getBoundingBox() {

      if (coords_.size() == 0) {
        return null;
      }

      double x1 = Double.POSITIVE_INFINITY, y1 = Double.POSITIVE_INFINITY;
      double x2 = Double.NEGATIVE_INFINITY, y2 = Double.NEGATIVE_INFINITY;

      for (int i = 1; i < coords_.size(); i++) {
        x1 = Math.min(x1, coords_.get(i).x);
        y1 = Math.min(y1, coords_.get(i).y);
        x2 = Math.max(x2, coords_.get(i).x);
        y2 = Math.max(y2, coords_.get(i).y);
      }

      return new Rectangle2D.Double(x1, y1, x2 - x1, y2 - y1);

    }
  }

  public static class Node {

    private Coordinate c_;
    private String id_;

    public Node(String id, Coordinate c) {
      id_ = id;
      c_ = c;
    }

    public String getID() {
      return id_;
    }

    public Coordinate getCoord() {
      return c_;
    }
  }
}
